package org.azmat.demojpa.Controller.JPA;

import org.azmat.demojpa.SQL.JPA.Entity.Students;
import org.azmat.demojpa.SQL.JPA.Service.StudentsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;

@Controller
@RequestMapping("/students")
public class StudentsController {
    @Autowired
    private StudentsService studentsService;

    @GetMapping("{id}")
    public ModelAndView getStudentsById(@PathVariable("id") int id){
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.addObject("students", studentsService.findById(id));
        modelAndView.setViewName("studentsList"); // view name
        return modelAndView;
    }

    @GetMapping("/ClassAll/{id}")
    public ModelAndView getAllStudents(@PathVariable("id") int id){
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.addObject("students", studentsService.findById(id));
        modelAndView.setViewName("studentsList"); // view name
        return modelAndView;
    }

    @GetMapping("/findStudentsCourse")
    public ModelAndView findStudentsCourse(){
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.addObject("students", studentsService.findAll());
        modelAndView.setViewName("studentsList"); // view name
        return modelAndView;
    }

    @GetMapping("/searchStudents")
    public ModelAndView searchStudents(Students students){
        ModelAndView modelAndView = new ModelAndView();
        List<Students> studentsList = studentsService.searchStudents(students);
        modelAndView.addObject("students", studentsList);
        modelAndView.setViewName("studentsList"); // view name
        return modelAndView;
    }
}
